package com.hotel.controller;

import com.hotel.pojo.Role;
import com.hotel.pojo.User;
import com.hotel.service.RoleService;
import com.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shkstart  后台管理用户类控制器
 * @create 2021-06-24 22:05
 */
@Controller
public class AdminController  {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleMapper;


    //跳转到用户管理界面
    @RequestMapping("/skip/toListUser")
    public String toListUser(Model model){
        List<User> userList = userService.list(null);
        model.addAttribute("emps",userList);
        List<Role> list = roleMapper.list(null);
        model.addAttribute("roles",list);
        return "admin/views/listUser";
    }

    //跳转到用户添加界面
    @RequestMapping("/skip/addUserPage")
    public String toAddUserPage(Model model){
        //查出权限信息
        List<Role> roles = roleMapper.toListRole();
        model.addAttribute("role",roles);
        return "admin/views/addUser";
    }

    //添加用户信息
    @PostMapping("/toAddUser")
    public String toAddUser(Model model, User user){
        System.out.println(user.toString());
        boolean save = userService.save(user);
        System.out.println("添加用户信息====>"+save);
        return "redirect:/skip/toListUser";
    }


    //跳转到用户修改界面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")long id,Model model){
        User byUserId = userService.getById(id);
        model.addAttribute("empId",byUserId);
        //查出权限信息
        List<Role> roles = roleMapper.toListRole();
        model.addAttribute("role",roles);
        return "admin/views/updateUser";
    }

    //修改用户信息
    @PostMapping("/toUpdateUser")
    public String toUpdateUser(Model model,User user){
        boolean i = userService.updateById(user);
        System.out.println("用户信息修改的结果为===>" + i);
        return "redirect:/skip/toListUser";
    }

    //删除用户信息
    @RequestMapping("/deleted/{id}")
    public String toDeletedUser(@PathVariable("id")long id){
        boolean b = userService.removeById(id);
        System.out.println("删除结果为====> "+b);
        return "redirect:/skip/toListUser";
    }

    //模糊查询用户信息
    @RequestMapping("/selectUser")
    public String toSelectUser(String username,Model model){
        System.out.println("这里是controller层！");
        List<User> userList = userService.selectUserName(username);
        model.addAttribute("emps",userList);
        return "admin/views/listUser";
    }

}
